package com.detrum;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.v4.content.FileProvider;

import com.detrum.bean.VersionBean;
import com.detrum.service.DownloadReceiveCall;
import com.detrum.sqlite.SqliteVersionOperate;
import com.detrum.utils.LogUtils;

import java.io.File;

public class DownloadReceiver extends BroadcastReceiver {
    private DownloadReceiveCall downloadReceiveCall;

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        String action = intent.getAction();
        if (action.equals("download")) {
            String verify = intent.getStringExtra("verify");
            String path = intent.getStringExtra("path");
            int length = intent.getIntExtra("length", 0);
            int progress = intent.getIntExtra("progress", 0);
            if (downloadReceiveCall != null) {
                if (progress != 0) {
                    downloadReceiveCall.downloadAppProgress(progress, length);
                }
                if (verify == null || verify.equals("fail")) {
                    downloadReceiveCall.downloadFail();
                }
            }
            if (path != null) {
                install(context, path);
            }
        } else if (action.equals("downloadDevice")) {
            String path = intent.getStringExtra("path");
            int length = intent.getIntExtra("length", 0);
            int progress = intent.getIntExtra("progress", 0);
            if (downloadReceiveCall != null) {
                if (path != null) {
                    downloadReceiveCall.downloadDeviceFinish(path);
                }
                if (progress != 0) {
                    downloadReceiveCall.downloadDeviceProgress(progress, length);
                }
            }
        } else if (action.equals("downloadFail")) {
            LogUtils.e("DownloadReceiver downloadFail downloadReceiveCall = " + downloadReceiveCall);
            if (downloadReceiveCall != null) {
                downloadReceiveCall.downloadFail();
            }
        }
    }

    public void setDownloadReceiveCall(DownloadReceiveCall downloadReceiveCall) {
        this.downloadReceiveCall = downloadReceiveCall;
    }

    private void install(Context context, String filePath) {
        File apkFile = new File(filePath);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            //版本大于 N ，开始使用 fileProvider 进行安装
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            Uri contentUri = FileProvider.getUriForFile(
                    context
                    , "com.detrum.fileprovider"
                    , apkFile);
            intent.setDataAndType(contentUri, "application/vnd.android.package-archive");
        } else {
            intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
        }
        context.startActivity(intent);
    }
}
